package zl.ordermanager.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import zl.ordermanager.service.DeliveryService;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.Delivery;
import zl.ordermanager.vo.EDeliveryMethod;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.Total;

@Controller
@RequestMapping("/delivery")
public class DeliveryController extends BaseController {

	@Autowired
	private DeliveryService deliveryService;

	@RequestMapping("/showList")
	public String showList(HttpSession session, PageHelper pageHelper, Model model) {
		Total total = new Total();
		DataGrid<Delivery> dataGrid = deliveryService.dataGrid(session, pageHelper, total);
		List<Delivery> deliveryList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("deliveryList", deliveryList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);
		model.addAttribute("total", total);
		model.addAttribute("deliveryMethodList", EDeliveryMethod.values());

		return "deliveryList";
	}

	@RequestMapping("/showAdd")
	public String showAdd(HttpSession session, Model model) {
		model.addAttribute("deliveryMethodList", EDeliveryMethod.values());
		model.addAttribute("currentMonth", new SimpleDateFormat("yyyy-M").format(new Date()));
		return "deliveryAdd";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(Delivery delivery) {
		try {
			delivery.setAudit("未审核");
			deliveryService.add(delivery);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}

		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/showEdit/{deliveryId}")
	public String showEdit(@PathVariable("deliveryId") Integer deliveryId, Model model) {
		Delivery delivery = deliveryService.get(deliveryId);

		model.addAttribute("delivery", delivery);
		model.addAttribute("deliveryMethodList", EDeliveryMethod.values());

		return "deliveryEdit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView edit(Delivery delivery) {
		try {
			deliveryService.edit(delivery);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}
		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/delete/{deliveryId}")
	public ModelAndView delete(@PathVariable("deliveryId") Integer deliveryId) {

		deliveryService.delete(deliveryId);

		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/audit/{deliveryId}")
	public ModelAndView audit(HttpSession session, @PathVariable("deliveryId") Integer deliveryId) {
		try {
            deliveryService.audit(session, deliveryId);
		} catch (Exception e) {
            return ajaxDoneError(e.getMessage());
        }

		return ajaxDoneSuccess("操作成功");
	}

}
